<?php

namespace app\tour\controller;

use think\Db;
use think\Request;
use app\tour\model\Admin as AdminModel;
use think\Session;

class Admin extends Common
{

    public function admin(){

        if (Request::instance()->isAjax()){

            $admin = AdminModel::getAdminList();

            return ['code'=>0,'msg'=>'','data'=>$admin];

        }else{
            $role = Db::name('tour_auth_group')->select();
            $this->assign('role',$role);
            return view();

        }

    }

    public function addAdmin(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();
            $post = $post['data'];


            //检查手机号是否存在
            $check = AdminModel::getAdminByTel($post['t_admin_tel']);

            if ($check){

                return ['code'=>1,'msg'=>'手机号已存在，请更换手机号'];
                die;

            }
            if(empty($post['role_id'])){
                return ['code'=>'请先添加角色'];
                exit;
            }
            $role_id = $post['role_id'];
            unset($post['role_id']);
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $post['password'] = $hasher->HashPassword($post['password']);

            $addData = [
                't_admin_tel'  => $post['t_admin_tel'],
                'password'     => $post['password'],
                'tour_id'      => Session::get('tour_id'),
                'pid'          => 1,
                'add_time'     => date("Y-m-d H:i:s"),
                'admin_rename' =>$post['admin_rename']
            ];

            $res = AdminModel::addAdmin($addData);

            if ($res){
                $group = array('uid'=>$res,'group_id'=>$role_id);
                Db::name('tour_auth_group_access')->insert($group);
                return ['code'=>0];

            }else{

                return [
                    'code'=>1,
                    'msg'=>'添加失败'
                ];

            }

        }

    }

    public function editAdmin(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $editData = [
                't_admin_tel' => $post['data']['t_admin_tel'],
                'admin_rename'=>$post['data']['admin_rename'],
            ];

            $res = AdminModel::editAdmin($post['data']['t_admin_id'],$editData);
            $role = Db::name('tour_auth_group_access')->where("uid",$post['data']['t_admin_id'])->setField('group_id',$post['data']['role_id']);

            if ($res !== false && $role !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function editAdminPassword(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $post = $post['data'];

            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $post['password'] = $hasher->HashPassword($post['password']);

            $editData = [
                'password'=>$post['password']
            ];

            $res = AdminModel::editAdmin($post['t_admin_id'],$editData);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function delAdmin(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $res = Db::name('tour_admin')->where('t_admin_id',$post['t_admin_id'])->delete();

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

    public function editAdminIsUse(){

        if (Request::instance()->isAjax()){

            $post = Request::instance()->post();

            $editData =[

                'is_use' => $post['is_use']

            ];

            $res = AdminModel::editAdmin($post['t_admin_id'],$editData);

            if ($res !== false){

                return ['code'=>0];

            }else{

                return ['code'=>1];

            }

        }

    }

}